package com.googlecode.asm_ide.compiler;

import java.awt.Color;
import java.awt.Container;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JTextPane;
import javax.swing.text.*;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 *
package com.googlecode.asm_ide.compiler;

/**
 *
 * @author philipp
 */
public class CompileFileJDialog extends javax.swing.JDialog {

    /**
     * Creates new form CompileFileJDialog
     */
    public CompileFileJDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        
        StyleContext context = new StyleContext();
        consoleOutputDocument = new DefaultStyledDocument(context);
        
        outputAttributeSet = new SimpleAttributeSet();
        StyleConstants.setFontFamily(outputAttributeSet, "Courier New");
        StyleConstants.setFontSize(outputAttributeSet, 12);
        StyleConstants.setForeground(outputAttributeSet, Color.black);
        
        errorAttributeSet = new SimpleAttributeSet();
        StyleConstants.setFontFamily(errorAttributeSet, "Courier New");
        StyleConstants.setFontSize(errorAttributeSet, 12);
        StyleConstants.setForeground(errorAttributeSet, Color.red);
        
        this.backJButton.setEnabled(false);
        this.finishJButton.setEnabled(false);
        this.nextJButton.setEnabled(false); 
        
        compilerJComboBox.addItem(new NASM());
        
        consoleOutputJTextPane.setStyledDocument(consoleOutputDocument);
    }

    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        filePathJLabel = new javax.swing.JLabel();
        changeFilePathJButton = new javax.swing.JButton();
        filePathJTextField = new javax.swing.JTextField();
        entryPointJLabel = new javax.swing.JLabel();
        entryPointJTextField = new javax.swing.JTextField();
        compilerJComboBox = new javax.swing.JComboBox();
        compilerJLabel = new javax.swing.JLabel();
        consoleOutputJLabel = new javax.swing.JLabel();
        backJButton = new javax.swing.JButton();
        jSeparator = new javax.swing.JSeparator();
        nextJButton = new javax.swing.JButton();
        cancelJButton = new javax.swing.JButton();
        finishJButton = new javax.swing.JButton();
        consoleOutputJScrollPane = new javax.swing.JScrollPane();
        consoleOutputJTextPane = new javax.swing.JTextPane();
        compileJButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Compile File");

        filePathJLabel.setText("File path:");

        changeFilePathJButton.setText("Change");
        changeFilePathJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                changeFilePathJButtonActionPerformed(evt);
            }
        });

        entryPointJLabel.setText("Entry point:");

        entryPointJTextField.setToolTipText("The entry point (procedure name) of the source file.");

        compilerJLabel.setText("Compiler:");

        consoleOutputJLabel.setText("Console Output:");

        backJButton.setText("Back");
        backJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backJButtonActionPerformed(evt);
            }
        });

        nextJButton.setText("Next");
        nextJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextJButtonActionPerformed(evt);
            }
        });

        cancelJButton.setText("Cancel");
        cancelJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelJButtonActionPerformed(evt);
            }
        });

        finishJButton.setText("Finish");
        finishJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                finishJButtonActionPerformed(evt);
            }
        });

        consoleOutputJScrollPane.setViewportView(consoleOutputJTextPane);

        compileJButton.setText("Compile");
        compileJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                compileJButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(backJButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(nextJButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 252, Short.MAX_VALUE)
                        .add(finishJButton)
                        .add(12, 12, 12)
                        .add(cancelJButton))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jSeparator))
                .addContainerGap())
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(filePathJLabel)
                            .add(entryPointJLabel)
                            .add(compilerJLabel))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(compilerJComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(0, 0, Short.MAX_VALUE))
                            .add(layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(filePathJTextField)
                                    .add(entryPointJTextField))
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(changeFilePathJButton))))
                    .add(layout.createSequentialGroup()
                        .add(consoleOutputJLabel)
                        .add(0, 0, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .add(consoleOutputJScrollPane)
                        .addContainerGap())
                    .add(compileJButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(filePathJLabel)
                    .add(changeFilePathJButton)
                    .add(filePathJTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(entryPointJTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(entryPointJLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(compilerJComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(compilerJLabel))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 121, Short.MAX_VALUE)
                .add(compileJButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(backJButton)
                    .add(nextJButton)
                    .add(finishJButton)
                    .add(cancelJButton))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jSeparator, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(consoleOutputJLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(consoleOutputJScrollPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 136, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void backJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backJButtonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_backJButtonActionPerformed

    private void nextJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextJButtonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_nextJButtonActionPerformed

    private void finishJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_finishJButtonActionPerformed
        this.dispose();
    }//GEN-LAST:event_finishJButtonActionPerformed

    private void cancelJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelJButtonActionPerformed
        this.dispose();
    }//GEN-LAST:event_cancelJButtonActionPerformed

    private void changeFilePathJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_changeFilePathJButtonActionPerformed
        jFileChooser = new JFileChooser();
        jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        jFileChooser.setFileFilter(new SourceFileFilter());
        jFileChooser.setMultiSelectionEnabled(false);
        if (jFileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            File f = jFileChooser.getSelectedFile();
            filePathJTextField.setText(f.getPath());
            finishJButton.setEnabled(true);
        }
    }//GEN-LAST:event_changeFilePathJButtonActionPerformed

    private void compileJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_compileJButtonActionPerformed
        Compiler c = (Compiler) compilerJComboBox.getSelectedItem();
        if(c.isAvailable()){
            if(jFileChooser != null && jFileChooser.getSelectedFile() != null) {
                c.compile(jFileChooser.getSelectedFile());
                Iterator it = c.getConsoleOutput().iterator();
                while(it.hasNext()){
                    try {
                        consoleOutputDocument.insertString(consoleOutputDocument.getLength(), it.next()+"\r\n", outputAttributeSet);
                    } catch (BadLocationException ex) {
                        Logger.getLogger(CompileFileJDialog.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                it = c.getErrorOutput().iterator();
                while(it.hasNext()){
                    try {
                        consoleOutputDocument.insertString(consoleOutputDocument.getLength(), it.next()+"\r\n", errorAttributeSet);
                    } catch (BadLocationException ex) {
                        Logger.getLogger(CompileFileJDialog.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            } else {
                try {
                    consoleOutputDocument.insertString(consoleOutputDocument.getLength(), "No file selected.\r\n", errorAttributeSet);
                } catch (BadLocationException ex) {
                    Logger.getLogger(CompileFileJDialog.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } else {
            try {
                consoleOutputDocument.insertString(consoleOutputDocument.getLength(), "Compiler not available.\r\n", errorAttributeSet);
            } catch (BadLocationException ex) {
                Logger.getLogger(CompileFileJDialog.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        compileJButton.setEnabled(false);
        
    }//GEN-LAST:event_compileJButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton backJButton;
    private javax.swing.JButton cancelJButton;
    private javax.swing.JButton changeFilePathJButton;
    private javax.swing.JButton compileJButton;
    private javax.swing.JComboBox compilerJComboBox;
    private javax.swing.JLabel compilerJLabel;
    private javax.swing.JLabel consoleOutputJLabel;
    private javax.swing.JScrollPane consoleOutputJScrollPane;
    private javax.swing.JTextPane consoleOutputJTextPane;
    private javax.swing.JLabel entryPointJLabel;
    private javax.swing.JTextField entryPointJTextField;
    private javax.swing.JLabel filePathJLabel;
    private javax.swing.JTextField filePathJTextField;
    private javax.swing.JButton finishJButton;
    private javax.swing.JSeparator jSeparator;
    private javax.swing.JButton nextJButton;
    // End of variables declaration//GEN-END:variables
    private StyledDocument consoleOutputDocument;
    private SimpleAttributeSet errorAttributeSet;
    private SimpleAttributeSet outputAttributeSet;
    private JFileChooser jFileChooser;
}